home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / SeqPups / appsrc / autoseq.src / xlate.C < prev   
C/C++ Source or Header  |  1996-07-05  |  7KB  |  257 lines

  1. //    ============================================================================
  2. //    xlate.C                                                            80 columns
  3. //    Reece Hart    (reece@ibc.wustl.edu)                                tab=4 spaces
  4. //    Washington University School of Medicine, St. Louis, Missouri
  5. //    This source is hereby released to the public domain.  Bug reports, code
  6. //    contributions, and suggestions are appreciated (to email address above).
  7. //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  8. //    % xlate {options_list} {filename}
  9. //    options_list ::= {'-'}{option} {options_list}
  10. //    option ::=
  11. //    fmt {ABI0,ABI1,ABI,SCF}
  12. //        Read in specified format; guess if not specified.
  13. //    o [string prefix]
  14. //        specifies output filename; if omitted, the output file is the input
  15. //        filename.SCF.
  16. //    ========================================|===================================
  17.  
  18. #include    "CTraceFile.H"
  19. #include    "CTrace.H"
  20. #include    "FileFormat.H"
  21. #include    <stdlib.h>
  22. #include    <string.h>
  23. #include    <iostream.h>
  24. #include    <stdio.h>
  25. #include    "RInclude.H"
  26. #include    "RInlines.H"
  27.  
  28. const char* VERSION        = "94.05.03";
  29. const short    FNLEN        = 200;                // max filename length
  30. const short SUFXLEN        = 5;                // max suffix length
  31. const char    HYPHEN        = '-';                // command line switch marker
  32. const int    digestNoProc = -1;                // returned if we shouldn't process
  33.  
  34. const char* fmtsw        = "fmt";            // format switch
  35. const char* helpsw        = "help";            // cl switches for: help
  36. const char* leftsw        = "l";                // left cutoff
  37. const char*    outfnsw        = "o";                // output filename
  38. const char* outfmtsw    = "of";                // output format
  39. const char* rightsw        = "r";                // right cutoff
  40. const char* versionsw    = "version";        // get version info
  41.  
  42. void    help(void);
  43. void    Fatal(char* error)
  44.     {
  45.     cerr << "xlate: FATAL: " << error << endl;
  46.     exit(1);
  47.     }
  48. void    Warning(char* error)
  49.     {
  50.     cerr << "xlate: WARNING: " << error << endl;
  51.     }
  52.  
  53. struct opts_t
  54.     {
  55.     char    outFN[FNLEN];
  56.     format_t fmt;
  57.     format_t outfmt;
  58.     tracepos left;
  59.     tracepos right;
  60.  
  61.             opts_t(void);
  62.             ~opts_t(void) {}
  63.     int        Digest(int argc, char* argv[]);
  64.     };
  65.  
  66. inline
  67. opts_t::opts_t(void):
  68.     fmt(unknown), outfmt(SCF),
  69.     left(0), right(0)
  70.     {
  71.     // ensure that all strings are 0-length
  72.     outFN[0]    =    NULL;
  73.     }
  74.  
  75. int
  76. opts_t::Digest(int argc, char** argv)
  77.     {
  78.     uint    argIndex;
  79.     char*    flag;
  80.     bool    hyphenflag = FALSE;            // if we remove a hyphen and get
  81.                                         // something we don't understand, then
  82.                                         // call help
  83.  
  84.     if (argc<2)
  85.         Fatal("xlate: bad command line; try % xlate -h");
  86.  
  87.     // scan for version request
  88.     for(argIndex=1;argIndex<=argc-1;argIndex++)
  89.         {
  90.         flag = argv[argIndex];
  91.         if (*flag == HYPHEN)        flag++;        // peel off the hyphen
  92.         if (strcmp(flag,versionsw)==0)
  93.             {
  94.             cout << "xlate" << endl
  95.                 << "  version " << VERSION
  96.                 << "  [made on " << __DATE__ << " " << __TIME__ << "]" << endl
  97.                 << "  Reece Hart (reece@ibc.wustl.edu)" << endl
  98.                 << "  translates chromatogram files to other formats" << endl
  99.                 << "  use '% xlate -help' for usage instructions" << endl
  100.                 << endl;
  101.             return digestNoProc;
  102.             }
  103.         }
  104.  
  105.     // scan for help
  106.     for(argIndex=1;argIndex<=argc-1;argIndex++)
  107.         {
  108.         flag = argv[argIndex];
  109.         if (*flag == HYPHEN)        flag++;        // peel off the hyphen
  110.         if (strcmp(flag,helpsw)==0)
  111.             { help(); return digestNoProc; }
  112.         }
  113.  
  114.     // return this value, which hopefully points to the first filename.
  115.     // NOTE: loop only through argc-2 so that at least one argument remains
  116.     // (the filename of the input file); we may stop before that if we don't
  117.     // understand a flag, and assume that this flag is the start of the filename
  118.     // list.
  119.     for(argIndex=1; argIndex<argc; argIndex++)
  120.         {
  121.         flag = argv[argIndex];
  122.         hyphenflag = FALSE;
  123.  
  124.         if (*flag == HYPHEN)
  125.             {
  126.             flag++;
  127.             hyphenflag = TRUE;
  128.             }
  129.  
  130.         // MODIFIERS
  131.         // These may seg fault if the user omits an argument at the
  132.         // end of the command line
  133.         if (strcmp(flag,fmtsw)==0)
  134.             {
  135.             // sequential search through tracefile format abbreviations to find
  136.             // match, or default to unknown format if not found
  137.             ++argIndex;
  138.             for(short i=0;i<(short)unknown;i++)
  139.                 if (strcmp(TRACEFILE_FORMATS[i].abbr,argv[argIndex])==0)
  140.                     break;
  141.             fmt = (format_t)i;
  142.             // upon loop exit, i is either the format value or unknown
  143.             continue;
  144.             }
  145.         if (strcmp(flag,leftsw)==0)
  146.             { left = atoi(argv[++argIndex]); continue; }
  147.         if (strcmp(flag,outfnsw)==0)
  148.             { strcpy(outFN,argv[++argIndex]); continue; }
  149.         if (strcmp(flag,outfmtsw)==0)
  150.             {
  151.             // sequential search through tracefile format abbreviations to find
  152.             // match, or default to unknown format if not found
  153.             ++argIndex;
  154.             for(short i=0;i<(short)unknown;i++)
  155.                 if (strcmp(TRACEFILE_FORMATS[i].abbr,argv[argIndex])==0)
  156.                     break;
  157.             outfmt = (format_t)i;
  158.             // upon loop exit, i is either the format value or unknown
  159.             continue;
  160.             }
  161.         if (strcmp(flag,rightsw)==0)
  162.             { right = atoi(argv[++argIndex]); continue; }
  163.  
  164.         // DEFAULT: unrecognized flags... we're done
  165.         if (hyphenflag)
  166.             // we've stripped off a hyphen, which means the user intended this
  167.             // to be a flag, but we didn't understand it.  Call help and tell
  168.             // our caller to not process.
  169.             {
  170.             char buf[100];
  171.             strcpy(buf,"Unrecognized flag '");strcat(buf,flag);strcat(buf,"'.");
  172.             Warning(buf);
  173.             help();
  174.             return digestNoProc;
  175.             }
  176.         break;
  177.         }
  178.  
  179.     return argIndex;                // we successfully read argIndex params
  180.     }
  181.  
  182. int
  183. main(int argc, char* argv[])
  184.     {
  185.     opts_t        options;
  186.     int            argr;
  187.     CTraceFile::error_t CTFError = CTraceFile::noError;
  188.  
  189.     argr = options.Digest(argc, argv);
  190.     if (argr == digestNoProc)
  191.         return 1;
  192.  
  193.     if (argc-argr != 1)
  194.         Fatal("bad command line arguments -- xlate -help for info");
  195.  
  196.     char*&        FN = argv[argr];
  197.     format_t    fmt = options.fmt;
  198.  
  199.     if (unknown == fmt)
  200.         fmt = FileFormat(FN);
  201.  
  202.     // read file
  203.     cout << "Reading '"<<FN<<"' ("<<FileFormatAbbr(fmt)<< ")..." << flush;
  204.     CTraceFile     tracefile(FN,fmt);
  205.     CTFError = tracefile.Error();
  206.     cout << "(" << CTFError << ")." << endl;
  207.     if (CTraceFile::noError != CTFError)
  208.         Fatal("Couldn't read file.");
  209.  
  210.     if (options.left !=0) tracefile.LeftCutoff(options.left);
  211.     if (options.right!=0) tracefile.RightCutoff(options.right);
  212.  
  213.     // scale file for writing SCF
  214.     for(uint i=A;i<NUM_BASES;i++)
  215.         {
  216.         CTrace<trace_t>&    trace = *(tracefile[i]);
  217.         cout << "Trace " << DNA_BASES[i] << endl;
  218.         cout << "min/max/mean:\t"
  219.              << trace.Min() << "/" << trace.Max() << "/" << trace.Mean()
  220.              << "...translated by " << -trace.Min() << endl;
  221.         trace.Translate(-trace.Min());
  222.         cout << "min/max/mean:\t"
  223.              << trace.Min() << "/" << trace.Max() << "/" << trace.Mean()
  224.              << "...scaled by " << MAX_SCF_TRACE_VALUE/trace.Max() << endl;
  225.         trace.Scale(MAX_SCF_TRACE_VALUE/trace.Max());
  226.         cout << "min/max/mean:\t"
  227.              << trace.Min() << "/" << trace.Max() << "/" << trace.Mean()<< endl;
  228.         }
  229.  
  230.     // read file
  231.     if (strlen(options.outFN)==0)
  232.         {
  233.         strcpy(options.outFN,FN);
  234.         strcat(options.outFN,".SCF");
  235.         }
  236.     cout << "Writing '"<<options.outFN<<"' as "
  237.         << FileFormatAbbr(options.outfmt) << " file..." << flush;
  238.     CTFError = tracefile.Write(options.outFN,options.outfmt);
  239.     cout << "(" << CTFError << ")." << endl;
  240.     if (CTraceFile::noError != CTFError)
  241.         Fatal("Couldn't write file.");
  242.  
  243.     exit(0);
  244.     }
  245.  
  246. void
  247. help(void)
  248.     {
  249. cout
  250. << "Most help displays give useful information about what a program\n"
  251. << "does and how to use it.  This one doesn't.  Sorry.\n"
  252. << "This program converts from ABI to SCF, including converting /raw/ ABI\n"
  253. << "data with the -fmt ABI0 flag.  xlate is currently unsupported, but the\n"
  254. << "intrepid are welcome to peruse the source to discover what it does.\n";
  255.     }
  256.  
  257.